package gui;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import util.Parser;
import util.Protocol;
import net.BackClient;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RegisterUI extends JFrame {
	private static final long serialVersionUID = 6758891080960583715L;
	private JPanel contentPane;
	
	private JTextField textField;
	
	private JPasswordField passwordField;
	
	private JPasswordField passwordField_1;
	
	BackClient backClient = new BackClient();
	
	Protocol protocol = new Protocol();
	
	Parser parser = new Parser();

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegisterUI frame = new RegisterUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public RegisterUI() {
		setTitle("注册");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 479, 277);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblpp = new JLabel("欢迎注册");
		lblpp.setFont(new Font("宋体", Font.PLAIN, 17));
		lblpp.setBounds(163, 29, 145, 32);
		contentPane.add(lblpp);
		
		JLabel label = new JLabel("用户名：");
		label.setFont(new Font("宋体", Font.PLAIN, 17));
		label.setBounds(63, 72, 76, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("密码：");
		label_1.setFont(new Font("宋体", Font.PLAIN, 17));
		label_1.setBounds(63, 105, 76, 15);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("确认密码：");
		label_2.setFont(new Font("宋体", Font.PLAIN, 17));
		label_2.setBounds(63, 144, 177, 15);
		contentPane.add(label_2);
		
		textField = new JTextField();
		textField.setBounds(221, 71, 112, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(221, 104, 112, 21);
		contentPane.add(passwordField);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(221, 143, 112, 21);
		contentPane.add(passwordField_1);
		
		JButton button = new JButton("确定");
		button.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				String nickname = textField.getText();
				String password1 = String.valueOf(passwordField.getPassword());
				String password2 = String.valueOf(passwordField_1.getPassword());
				
				if(password1.equals(password2))
				{
					String information = nickname + "," +password2;
					String massage = protocol.getRegisterInformation(information);
					String backmassage = backClient.ReseiveMassage();
					
					boolean issuccess = parser.isRegisterok(backmassage);
					if(issuccess)
					{
						JOptionPane.showMessageDialog(null, "注册成功");
						LogInUI inUI = new LogInUI();
						inUI.setVisible(true);
						RegisterUI.this.dispose();
					}
					else
					{
						JOptionPane.showMessageDialog(null, "注册失败");
						textField.setText("");
					}
				}
				else
				{
					JOptionPane.showMessageDialog(null, "请输入一致的密码");
					passwordField.setText("");
					passwordField_1.setText("");
				}
				
			}
		});
		button.setBounds(360, 190, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("返回");
		button_1.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				RegisterUI.this.setVisible(false);
				LogInUI inUI = new LogInUI();
				inUI.setVisible(true);
			}
		});
		button_1.setBounds(10, 190, 93, 23);
		contentPane.add(button_1);
	}
}
